/**
 * 
 */
package messaage;

public abstract class Message {

	protected String username;

	public abstract String toString();

	public String getUsername() {
		return username;
	}
	
	public static Message fromString(String msg) {
		Message ans = null;
		if (msg.length() >= 7 && msg.substring(0, 7).equals("Connect")) {
			String tmpS = msg.substring(8);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 1) {
				System.out.println("bad connect input");
				return null;
			}

			ans = new ConnectMsg("");
		}
		if (msg.length() >= 10 && msg.substring(0, 10).equals("Disconnect")) {
			String tmpS = msg.substring(11);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 1) {
				System.out.println("bad disconnect input");
				return null;
			}

			ans = new DisconnectMsg("");
		}
		if (msg.length() >= 9 && msg.substring(0, 9).equals("AddFriend")) {
			String tmpS = msg.substring(10);
			String[] lInfo = tmpS.split("\n");
			String friendName = lInfo[0].split(":")[1];
			ans = new AddFriendMsg("", friendName);
		}
		if (msg.length() >= 12 && msg.substring(0, 12).equals("RemoveFriend")) {
			String tmpS = msg.substring(13);
			String[] lInfo = tmpS.split("\n");
			String friendName = lInfo[0].split(":")[1];
			ans = new RemoveFriendMsg("", friendName);
		}
		if (msg.length() >= 5 && msg.substring(0, 5).equals("Login")) {
			String tmpS = msg.substring(6);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 2) {
				System.out.println("bad login input");
				return null;
			}
			String uName = lInfo[0].split(":")[1];
			String pwd = lInfo[1].split(":")[1];
			ans = new LoginMsg(uName, pwd);
		}
		if (msg.length() >= 6 && msg.substring(0, 6).equals("Logout")) {
			ans = new LogoutMsg("");
		}
		if (msg.length() >= 8 && msg.substring(0, 8).equals("Register")) {
			String tmpS = msg.substring(9);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 6) {
				System.out.println("bad register input");
				return null;
			}
			String fName = lInfo[0].split(":")[1];
			String lName = lInfo[1].split(":")[1];
			String eMail = lInfo[2].split(":")[1];
			String gender = lInfo[3].split(":")[1];
			String uName = lInfo[4].split(":")[1];
			String pwd = lInfo[5].split(":")[1];
			ans = new RegisterMsg(fName, lName, eMail, gender.charAt(0), uName, pwd);
		}
		if (msg.length() >= 8 && msg.substring(0, 8).equals("AddForum")) {
			String tmpS = msg.substring(9);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 2) {
				System.out.println("bad addforum input");
				return null;
			}
			String subject = lInfo[1].split(":")[1];
			ans = new AddForumMsg("", subject);
		}
		if (msg.length() >= 13 && msg.substring(0, 13).equals("ForumsListReq")) {
			ans = new GetListOfForumsMsg("");
		}
		if (msg.length() >= 14 && msg.substring(0, 14).equals("ForumTopicsMsg")) {
			String tmpS = msg.substring(15);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 2) {
				System.out.println("bad ForumTopicsMsg input");
				return null;
			}
			String fID = lInfo[1].split(":")[1];
			ans = new GetForumTopicsMsg("", fID);
		}
		if (msg.length() >= 12 && msg.substring(0, 12).equals("CreateThread")) {
			String tmpS = msg.substring(13);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 4) {
				System.out.println("bad createthread input");
				return null;
			}
			String fID = lInfo[1].split(":")[1];
			String title = lInfo[2].split(":")[1];
			String body = lInfo[3].split(":")[1];
			ans = new CreateThreadMsg("", fID, title, body);
		}
		if (msg.length() >= 10 && msg.substring(0, 10).equals("AddPostMsg")) {
			String tmpS = msg.substring(11);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 5) {
				System.out.println("bad AddPostMsg input");
				return null;
			}
			String uName = lInfo[0].split(":")[1];
			String fID = lInfo[1].split(":")[1];
			String postID = lInfo[2].split(":")[1];
			String body = lInfo[4].split(":")[1];
			String topicName = lInfo[3].split(":")[1];
			ans = new AddPostMsg(uName, fID, postID, topicName, body);
		}
		if (msg.length() >= 10 && msg.substring(0, 10).equals("ThreadsMsg")) {
			String tmpS = msg.substring(11);
			String[] lInfo = tmpS.split("\n");
			if (lInfo.length != 3) {
				System.out.println("bad ThreadsMsg input");
				return null;
			}
			String fID = lInfo[1].split(":")[1];
			String threadID = lInfo[2].split(":")[1];
			ans = new GetThreadMsg("", fID, threadID);
		}
		if (msg.length() >= 9 && msg.substring(0, 14).equals("FriendsListMsg")) {
			String tmpS = msg.substring(15);
			String[] lInfo = tmpS.split("\n");
			String uName = lInfo[0].split(":")[1];
			ans = new GetFriendsListMsg(uName);
		}

		return ans;
	}
}
